package com.android.nearshop.app.widget;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

import com.android.nearshop.R;
import com.android.nearshop.app.activity.shop.ShopDetailActivity;
import com.android.nearshop.io.protocol.in.BannerAction.ADV_TYPE;
import com.android.nearshop.io.protocol.out.Adv;
import com.android.nearshop.util.Constant;
import com.android.nearshop.util.ImageLoader;
import com.android.nearshop.util.ImageLoader.Imager;

/*********************************************************
 * @author : zhaohua
 * @version : 2012-8-21
 * @see : 广告图片
 * @Copyright : copyrights reserved by personal 2007-2011
 **********************************************************/
public class AdvView extends ImageView implements OnClickListener
{
    private static final String TAG = "AdvView";
    private Adv mData;
     
    public AdvView(Context context)
    {
        super(context);
    }
   
    public AdvView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }
   
    public void bindData(Adv data, int width)
    {
        this.mData = data;
        setOnClickListener(this);
        int size = data.advType == ADV_TYPE.MAIN.ordinal() ? width : 0;
        ImageLoader.getInstance().DisplayImage(new Imager(mData.advImgUrl, this, size, R.drawable.icon_main_adv_d));
    }
    
    @Override
    public void onClick(View v)
    {
        Log.d(TAG, "aadv click " + mData.advId);
        if(mData.linkType != null && 
          (mData.linkType.equals("shopDetail") || mData.linkType.equalsIgnoreCase("Shop")))
        {
            if(mData.linkData != null)
            {
                int shopid =  0;
                try {
                    shopid = Integer.parseInt(mData.linkData);
                }catch (Exception e) 
                {
                    Log.e(TAG, " linkData error!" );
                }
                if(shopid > 0)
                {
                    Intent intent = new Intent();
                    intent.setClass(getContext(),ShopDetailActivity.class);
                    intent.putExtra(Constant.TAG_DETAIL_SHOPID, shopid);
                    intent.putExtra(Constant.TAG_DISCOUNT, "");
                    
                    getContext().startActivity(intent);
                }
            }
        }
    }
}
